package org.aileen.trans.controller;


import lombok.extern.slf4j.Slf4j;
import org.aileen.trans.service.UserService;
import org.apache.ibatis.transaction.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired//注入数据源的事务管理器对象
    private DataSourceTransactionManager transactionManager;

    @Autowired
    private TransactionDefinition transactionDefinition;

    @Autowired
    private UserService userService;

    @RequestMapping("/registry")
    public Boolean registry(String name , String password){

        //开启事务
        TransactionStatus transaction = transactionManager.getTransaction(transactionDefinition);

        Integer result = userService.insertUser(name,password);
        log.info("数据影响行数: " + result );

//        //回滚事务
//        transactionManager.rollback(transaction);

        //提交事务
        transactionManager.commit(transaction);
        return true;
    }
}
